#!/bin/bash
#(c) Copyright 2009 Barry Kauler.
#110819 support/findpkgs is new script to find all pkgs to be used in Puppy build.
#120501 support arm build.
#120515 build from "gentoo" binary tarballs (refer support/gentoo).
#120719 support raspbian.

export LANG=C #faster.
[ $1 ] && XPKG=$1 #download specific pkg
MWD=$PWD

. ./_00build.conf
. ./_00func
. ./DISTRO_SPECS
source_compat_repos  # ./DISTRO_COMPAT_REPOS-
source_pkgs_specs    # ./DISTRO_PKGS_SPECS-
. ./DISTRO_PET_REPOS #has PACKAGELISTS_PET_ORDER
set_binaries_var     #ex: BINARIES=tgz_txz-14.2 BINARIES=deb-xenial
create_local_repos_dirs
mkdir -p packages-${BINARIES}
mkdir -p packages-pet
rm -f DOWNLOAD-FAILS-* DOWNLOAD-ERRORS 2>/dev/null
wget --help | grep -q '\-\-show\-progress' && WGET_SHOW_PROGRESS='-q --show-progress'

function do_exit() { #$1: msg
	kill `cat /tmp/1download_controlpanel_pid` &>/dev/null
	rm -f /tmp/1download_controlpanel_pid &>/dev/null
	echo -e "\n${1}"
	echo -e "\nScript finished."
	[ "$WOOF_GUI" ] && { echo -n "Press ENTER key to exit: " ; read zzz ; }
	exit
}

##################################
#120501 support arm build, download sd images...
if [ "$DISTRO_TARGETARCH" = "arm" -a "$BUILD_ZIP" != "yes" ];then
 mkdir -p sd-skeleton-images
 echo
 echo 'Please wait, probing for online SD skeleton image files...'
 wget -4 -t 2 -T 20 --waitretry=20 --spider --recursive --level=1 --accept '*.xz' -S http://distro.ibiblio.org/puppylinux/arm/sd-skeleton-images/ 2>&1 | grep '\.xz$' | rev | cut -f 1 -d '/' | rev > /tmp/1download-sd-images-probe
 sync
 if [ ! -s /tmp/1download-sd-images-probe ];then
  echo 'Sorry, could not access any SD images at http://distro.ibiblio.org/puppylinux/arm/sd-skeleton-images/'
  echo 'Press ENTER to continue script regardless: '
  read keepgoing
  [ "$keepgoing" != "" ] && exit
 else
  #ask which sd-image to download...
  ALREADYDOWN="`ls -1 sd-skeleton-images | grep 'xz$'`"
  [ "$ALREADYDOWN" = "" ] && ALREADYDOWN='none'
  SDIMAGEBUTTONS=''
  for ONESDIMAGE in `cat /tmp/1download-sd-images-probe | sort -u | tr '\n' ' '`
  do
   [ -f sd-skeleton-images/$ONESDIMAGE ] && continue #already downloaded.
   SDIMAGEBUTTONS="${SDIMAGEBUTTONS}<radiobutton><label>${ONESDIMAGE}</label><variable>RADIO_URL_${ONESDIMAGE}</variable></radiobutton>"
  done
  if [ "$SDIMAGEBUTTONS" != "" ];then
   export SDIMAGE_DIALOG="<window title=\"SD Image Downloader: choose file\" icon-name=\"gtk-about\" window_position=\"1\">
   <vbox>
    <text><label>These are already downloaded:</label></text>
    <text><label>\"${ALREADYDOWN}\"</label></text>
    <text><label>Please choose an SD-image file that you would like to download (Click Cancel button to not download any):</label></text>
    <frame>
     ${SDIMAGEBUTTONS}
    </frame>
    <hbox>
     <button ok></button>
     <button cancel></button>
    </hbox>
   </vbox>
   </window>"
   while [ 1 ];do
    RETPARAMS="`gtkdialog3 --program=SDIMAGE_DIALOG`"
    SDIMAGE_FILE=''
    [ "`echo "$RETPARAMS" | grep '^EXIT' | grep 'OK'`" != "" ] && SDIMAGE_FILE="`echo "$RETPARAMS" | grep 'RADIO_URL_' | grep '"true"' | cut -f 1 -d '=' | cut -f 3-9 -d '_'`"
    if [ "$SDIMAGE_FILE" ];then
     cd sd-skeleton-images
     wget -4 http://distro.ibiblio.org/puppylinux/arm/sd-skeleton-images/$SDIMAGE_FILE
     wget -4 http://distro.ibiblio.org/puppylinux/arm/sd-skeleton-images/${SDIMAGE_FILE}.md5.txt
     sync
     cd ..
     echo "...${SDIMAGE_FILE} downloaded to folder sd-skeleton-images"
     if [ -s ${SDIMAGE_FILE}.md5.txt ];then
      CORRECTMD5="`cat ${SDIMAGE_FILE}.md5.txt | cut -f 1 -d ' '`"
      DLMD5="`md5sum ${SDIMAGE_FILE} | cut -f 1 -d ' '`"
      if [ "$CORRECTMD5" != "$DLMD5" ];then
       echo "...however, the md5sum is incorrect. Deleting downloaded file..."
       rm -f $SDIMAGE_FILE
       echo -n "Press ENTER only to continue script, any other key to try download again..."
       read tryagain
       [ "$tryagain" != "" ] && continue    
      fi
     fi
    fi
    break
   done
  fi
  
 fi
fi

run_findpkgs #find all packages that will be used in the Puppy build...

#==============================================================================

function download_controlpanel() {
	export DOWNLOAD_CONTROLPANEL='
<window title="1download control panel" icon-name="gtk-dialog-warning">
 <vbox>
  <hbox>
   <text><label>Pause after current package has downloaded:</label></text>
   <button>
      <label>Pause</label>
      <action>echo 'Pause' > /tmp/1download-status-request</action>
   </button>
  </hbox>
  <hbox>
   <text><label>Resume downloading if currently paused:</label></text>
   <button>
      <label>Resume</label>
      <action>echo 'Resume' > /tmp/1download-status-request</action>
   </button>
  </hbox>
  <hbox>
   <text><label>Quit 1download script after current pkg has download:</label></text>
   <button>
      <label>Quit</label>
      <action>echo 'Quit' > /tmp/1download-status-request</action>
      <action type="exit">Exit</action>
   </button>
  </hbox>
  <text><label>Note: If you Quit, 1download can be run again and will continue from where it left off. That is, previously downloaded packages will not be re-downloaded.</label></text>
 </vbox>
</window>
'
	gtkdialog --program=DOWNLOAD_CONTROLPANEL &
	echo -n $! > /tmp/1download_controlpanel_pid
}

#==============================================================================

REPOS_DISTRO_COMPAT=$(echo "$REPOS_DISTRO_COMPAT" | tr ' ' '\n')
PET_REPOS=$(echo "$PET_REPOS" | tr ' ' '\n')

echo -e "\nDownloading packages..."
if [ $XPKG ] ; then
	XFILE=/tmp/zz_downloadpkg
	grep ":${XPKG}:" status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION} | sort > /tmp/zz_downloadpkg
else
	#start the gui controlpanel...
	echo 'Run' > /tmp/1download-status-request
	download_controlpanel
	XFILE=status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION}
fi

echo "" > $MWD/status/download_list
LINECNT=1
cat $XFILE | while read ONEDBENTRY
do
 #120812 optional subcategory...
 #ONEDBENTRY=:GENERICNAME:|PETorCOMPAT|DBFILE|pkgname|nameonly|version|pkgrelease|category[;subcategory]|size|path|fullfilename|dependencies|description|compileddistro|compiledrelease|repo|
 #...pkgname onwards is the full db entry for a package.
 IFS="|" read -r F1 F2 F3 F4 F5 F6 F7 F8 F9 F10 F11 F12 F13 <<< "$ONEDBENTRY"
 LINECNT=$(($LINECNT + 1))
 PETorCOMPAT="$F2" #ex: compat
 if [ "$PETorCOMPAT" != "pet" -a "$PETorCOMPAT" != "compat" ];then
  echo -e "\nSyntax ERROR in status/findpkgs_FINAL_PKGS-${DISTRO_BINARY_COMPAT}-${DISTRO_COMPAT_VERSION},"
  echo "line number ${LINECNT}:"
  echo "$ONEDBENTRY"
  echo -n "Ignoring this line, press ENTER to continue (CTRL-C to abort): "
  read KEEPITGOING
  continue
 fi
 DBFILE="$F3"           #ex: Packages-puppy-5-official
 DB_genericname="$F1"	#ex: abiword
 DB_pkgname="$F4"       #ex: abiword-2.3.4-w5
 DB_fullfilename="$F11" #ex: abiword-2.3.4-w5.pet
 DB_path="$F10"         #ex: pet_packages-5
 if [ ! "$DB_path" ];then
  ONLINESUBDIR="`echo -n "$DBFILE" | cut -f 3 -d '-'`"   #ex: 5
  if [ "$PETorCOMPAT" = "pet" ];then #120515
   DB_path="pet_packages-${ONLINESUBDIR}"
  else #compat binary tarballs
   DB_path="compat_packages-${ONLINESUBDIR}"
  fi
 fi
 
 SUCCESS='no'

 if [ "$PETorCOMPAT" = "pet" ];then
  LFILE=DOWNLOAD-FAILS-PET
  FILE_DIR=packages-pet
 else #-- compat
  LFILE=DOWNLOAD-FAILS-${BINARIES}
  FILE_DIR=packages-${BINARIES}
 fi

 cd ${MWD}

 if [ -f "${FILE_DIR}/${DB_fullfilename}" ] ; then
   echo -n " ${DB_fullfilename}" ; SUCCESS='yes'
   continue
 fi
 echo -e "\n* Downloading ${DB_fullfilename}"

 cd ${FILE_DIR}

 if [ "$PETorCOMPAT" = "pet" ];then
  XREPOS="$PET_REPOS"
 else #-- compat
  XREPOS="$REPOS_DISTRO_COMPAT"
 fi

 # have to give priority to the repo $DBFILE is in
 # $DBFILE ex: Packages-slackware-14.2-official
 # z|http://mirror.aarnet.edu.au/pub/slackware/slackware-14.2|Packages-slackware-14.2-official
 REPOS="$(echo "$XREPOS" | grep "|${DBFILE}$")
$(echo "$XREPOS" | grep -v "|${DBFILE}$")"
 URLS="$(echo "$REPOS" | cut -f 2 -d '|')"

 for ONE_URL in ${URLS}
 do
    [ -f "$DB_fullfilename" ] && { SUCCESS='yes' ; break ; }
    REMOTE_PATH=${ONE_URL}/${DB_path}/${DB_fullfilename}
    echo "  - from ${REMOTE_PATH%/*}"
    wget -c ${WGET_SHOW_PROGRESS} --timeout=15 ${REMOTE_PATH} || {
       echo "  failed (file not found on server or could not finish download)"
       rm -f ${DB_fullfilename}
       continue
    }
    #-
    ${MWD}/rootfs-skeleton/usr/local/petget/verifypkg.sh ${DB_fullfilename}
    [ $? -eq 0 ] && SUCCESS='yes'
    #-
    if [ "$SUCCESS" = "no" ] ; then
       echo "  failed (file not found or corrupted file)"
       rm -f ${DB_fullfilename} 2>/dev/null
    fi
    #--
    [ "$SUCCESS" = "yes" ] && { echo "  success" ; echo "${DB_genericname}" >> $MWD/status/download_list ; break ; }
 done

 cd ${MWD}

 [ "$SUCCESS" = "no" ] && echo "DOWNLOAD FAIL: ${DB_fullfilename}"
 [ "$SUCCESS" = "no" ] && echo "$DB_fullfilename" >> ${LFILE}

 #check controlpanel, whether need to pause, resume or quit...
 [ -f /tmp/1download-status-request ] || continue
 CP_STATUS_REQ="`cat /tmp/1download-status-request`" #Run, Pause, Resume, Quit
 pausecnt=0; PIDCP=0
 while [ "$CP_STATUS_REQ" = "Pause" ];do
  #echo "Status: paused ${pausecnt}seconds"
  if [ $PIDCP -eq 0 ];then
   yaf-splash -close never -placement top -bg orange -text "1download script is currently in paused state" &
   PIDCP=$!
  fi
  sleep 5
  pausecnt=`expr $pausecnt + 5`
  CP_STATUS_REQ="`cat /tmp/1download-status-request`"
 done
 [ $PIDCP -ne 0 ] && kill $PIDCP
 [ "$CP_STATUS_REQ" = "Quit" ] && break 2 #pipe into while above is a separate process, need '2' to break out.

done

[ $XPKG ] && echo && exit
[ "$CP_STATUS_REQ" = "Quit" ] && do_exit "1download aborted."
do_exit ""

###END###
